using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Microsoft.ApplicationBlocks.Data;

namespace Customs.Components.SXXK.TOKHAI
{
    public class DieuChinhToKhai
    {
        /// <summary>
        /// Lay Thong Tin dieu chinh thue cua mot to khai
        /// </summary>
        /// <param name="maHaiQuan"></param>
        /// <param name="maloaihinh"></param>
        /// <param name="sotk"></param>
        /// <param name="namdk"></param>
        /// <returns></returns>
        public static DataSet GetDanhSachDieuChinhOfToKhai(string maHaiQuan, string maloaihinh, int sotk, short namdk)
        {
            string connectionString = ConnectionManager.GetConnectionString("SXXK", maHaiQuan);
            string query = "SELECT     dbo.DDIEUCHINH.* from dbo.DDIEUCHINH where MA_HQ=@MA_HQ and MA_LH=@MA_LH AND NAMDK=@NAMDK AND SOTK=@SOTK";

            SqlParameter[] parameters = new SqlParameter[4];

            parameters[0] = new SqlParameter("@Ma_HQ", SqlDbType.Char);
            parameters[0].Size = 6;
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = maHaiQuan;

            parameters[1] = new SqlParameter("@MA_LH", SqlDbType.Char);
            parameters[1].Size = 5;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = maloaihinh;

            parameters[2] = new SqlParameter("@SOTK", SqlDbType.Int);
            parameters[2].Value = sotk;

            parameters[3] = new SqlParameter("@NAMDK", SqlDbType.SmallInt);
            parameters[3].Value = namdk;

            return SqlHelper.ExecuteDataset(connectionString, CommandType.Text, query, parameters);

        }
        /// <summary>
        /// Lay Thong tin dieu chinh thue cua to khai cua mot doanh nghiep
        /// </summary>
        /// <param name="maHaiQuan"></param>
        /// <param name="maDoanhNghiep"></param>
        /// <returns></returns>
        public static DataSet GetDanhSachDieuChinh(string maHaiQuan, string maDoanhNghiep)
        {
            string connectionString = ConnectionManager.GetConnectionString("SXXK", maHaiQuan);
            string query = "SELECT     dbo.DDIEUCHINH.* " +
                           "FROM         dbo.DTOKHAIMD INNER JOIN " +
                           "dbo.DDIEUCHINH ON dbo.DTOKHAIMD.SOTK = dbo.DDIEUCHINH.SOTK AND dbo.DTOKHAIMD.MA_LH = dbo.DDIEUCHINH.MA_LH AND " +
                           "dbo.DTOKHAIMD.MA_HQ = dbo.DDIEUCHINH.MA_HQ AND dbo.DTOKHAIMD.NAMDK = dbo.DDIEUCHINH.NAMDK " +
                           "where dbo.DTOKHAIMD.MA_HQ=@Ma_HQ AND dbo.DTOKHAIMD.MA_DV=@MA_DV";

            SqlParameter[] parameters = new SqlParameter[2];

            parameters[0] = new SqlParameter("@Ma_HQ", SqlDbType.Char);
            parameters[0].Size = 6;
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = maHaiQuan;

            parameters[1] = new SqlParameter("@Ma_DV", SqlDbType.VarChar);
            parameters[1].Size = 14;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = maDoanhNghiep;

            return SqlHelper.ExecuteDataset(connectionString, CommandType.Text, query, parameters);

        } 

        public static string [] WSGetDanhSachDieuChinh(string maHaiQuan, string maDoanhNghiep)
        {
            DataSet dsThongTinDieuChinh = GetDanhSachDieuChinh(maHaiQuan, maDoanhNghiep);
            DataSet dsHangDieuChinh = HangDieuChinh.GetDanhSachHangDieuChinh(maHaiQuan, maDoanhNghiep);
            DataSet dsHangDieuChinhChiTiet=HangDieuChinh.GetDanhSachHangDieuChinhChiTiet(maHaiQuan, maDoanhNghiep);
            string[] stXML = new string[6];
            if (dsThongTinDieuChinh.Tables[0].Rows.Count == 0)
                return null;
            stXML[0] = dsThongTinDieuChinh.GetXml();
            stXML[1] = dsHangDieuChinh.GetXml();
            stXML[2] = dsHangDieuChinhChiTiet.GetXml();
            if (dsHangDieuChinh.Tables[0].Rows.Count == 0)
                stXML[1] = "";
            if (dsHangDieuChinhChiTiet.Tables[0].Rows.Count == 0)
                stXML[2] = "";
            stXML[3] = dsThongTinDieuChinh.GetXmlSchema();
            stXML[4] = dsHangDieuChinh.GetXmlSchema();
            stXML[5] = dsHangDieuChinhChiTiet.GetXmlSchema();
            return stXML;
        }

        public static string[] WSGetDanhSachDieuChinhOfToKhai(string maHaiQuan, string maloaihinh, int sotk, short namdk)
        {
            DataSet dsThongTinDieuChinh = GetDanhSachDieuChinhOfToKhai(maHaiQuan, maloaihinh, sotk, namdk);
            DataSet dsHangDieuChinh = HangDieuChinh.GetDanhSachHangDieuChinhOfToKhai(maHaiQuan, maloaihinh, sotk, namdk);
            DataSet dsHangDieuChinhChiTiet = HangDieuChinh.GetDanhSachChiTietHangDieuChinhOfToKhai(maHaiQuan, maloaihinh, sotk, namdk);
            string[] stXML = new string[6];
            if (dsThongTinDieuChinh.Tables[0].Rows.Count == 0)
                return null;
            stXML[0] = dsThongTinDieuChinh.GetXml();
            stXML[1] = dsHangDieuChinh.GetXml();
            stXML[2] = dsHangDieuChinhChiTiet.GetXml();
            if (dsHangDieuChinh.Tables[0].Rows.Count == 0)
                stXML[1] = "";
            if (dsHangDieuChinhChiTiet.Tables[0].Rows.Count == 0)
                stXML[2] = "";
            stXML[3] = dsThongTinDieuChinh.GetXmlSchema();
            stXML[4] = dsHangDieuChinh.GetXmlSchema();
            stXML[5] = dsHangDieuChinhChiTiet.GetXmlSchema();
            return stXML;
        }
    }
}
